package com.hwd.gmall.item.service.impl;

import com.hwd.gmall.item.service.ItemService;
import com.hwd.gmall.model.product.BaseCategoryView;
import com.hwd.gmall.model.product.SkuImage;
import com.hwd.gmall.model.product.SkuInfo;
import com.hwd.gmall.model.product.SpuSaleAttr;
import com.hwd.gmall.product.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 商品详情服务service的实现类
 *
 * @author 黄伟东/Victor
 * @date 2022/4/21 23:33
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 根据skuId查询sku的详细信息,用于生成sku的详情页面
     *
     * @param skuId skuId
     * @return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> getItemInfo(Long skuId) {

        // 参数校验
        if (skuId == null) {
            throw new RuntimeException("参数不能为空");
        }

        // 初始化返回结果
        Map<String, Object> result = new HashMap<>(16);

        // 查询skuInfo的信息
        CompletableFuture<SkuInfo> future = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            // 判断商品是否存在，若不存在直接返回空
            if (skuInfo == null || skuInfo.getId() == null) {
                return null;
            }
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        // 若商品存在查询分类信息
        CompletableFuture<Void> future1 = future.thenAcceptAsync(skuInfo -> {
            // 判断商品是否存在，若不存在直接返回空
            if (skuInfo == null || skuInfo.getId() == null) {
                return;
            }
            BaseCategoryView category = productFeignClient.getCategory(skuInfo.getCategory3Id());
            result.put("category", category);
        }, threadPoolExecutor);

        // 查询商品的图片信息
        CompletableFuture<Void> future2 = future.thenAcceptAsync(skuInfo -> {
            // 判断商品是否存在，若不存在直接返回空
            if (skuInfo == null || skuInfo.getId() == null) {
                return;
            }
            List<SkuImage> skuImageList = productFeignClient.listSkuImages(skuId);
            result.put("skuImageList", skuImageList);
        }, threadPoolExecutor);

        // 查询商品的价格
        CompletableFuture<Void> future3 = future.thenAcceptAsync(skuInfo -> {
            // 判断商品是否存在，若不存在直接返回空
            if (skuInfo == null || skuInfo.getId() == null) {
                return;
            }
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            result.put("skuPrice", skuPrice);
        }, threadPoolExecutor);

        // 查询销售属性信息，表示当前sku的销售属性值是哪个
        CompletableFuture<Void> future4 = future.thenAcceptAsync(skuInfo -> {
            // 判断商品是否存在，若不存在直接返回空
            if (skuInfo == null || skuInfo.getId() == null) {
                return;
            }
            List<SpuSaleAttr> spuSaleAttrList =
                    productFeignClient.listSpuSaleAttrBySpuIdAndSkuId(skuInfo.getSpuId(), skuId);
            result.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);

        // 页面跳转使用的键值对
        CompletableFuture<Void> future5 = future.thenAcceptAsync(skuInfo -> {
            // 判断商品是否存在，若不存在直接返回空
            if (skuInfo == null || skuInfo.getId() == null) {
                return;
            }
            Map<String, String> skuIdsValueIds = productFeignClient.getSkuIdsValueIdsMap(skuInfo.getSpuId());
            result.put("skuIdsValueIds", skuIdsValueIds);
        }, threadPoolExecutor);

        // 等待所有任务执行完成后再返回结果
        CompletableFuture.allOf(future, future1, future2, future3, future4, future5).join();

        return result;
    }
}
